﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TinyGet.Models;

namespace TinyGet.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        TinyGetRepository repo = new TinyGetRepository();

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetLink(String hash)
        {
            TinyLink link = repo.getLink(hash);
            if (String.IsNullOrEmpty(link.Hash))
            {
                ViewData["hash"] = hash;
                return View("NotFound");
            }
            else
            {
                return View("Link", link);
            }
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public JsonResult Create(string url, string custom)//still need to add the custom name column to the DB and allow it to be searched for in the GetLink()
        {
            if (custom != null)
            {
                string hash = repo.addLink(url, custom);
                return Json(new { data = "http://tinyget.com/" + hash });
            }
            else
            {
                string hash = repo.addLink(url, null);
                return Json(new { data = "http://tinyget.com/" + hash });
            }


        }

    }
}
